/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.filippudak.progresspieview.sample.slice;

import com.filippudak.progresspieview.ProgressPieView;
import com.filippudak.progresspieview.sample.ResourceTable;
import com.filippudak.progresspieview.sample.util.ResUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Slider;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;

import java.util.Optional;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class MainAbilitySlice extends AbilitySlice {
    private static final int MARGIN = 8;

    private ProgressPieView mProgressPieView;
    private ProgressPieView mProgressPieViewInverted;
    private ProgressPieView mProgressPieViewXml;
    private ProgressPieView mProgressPieViewCode;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // Code versionR
        Optional<Display> dm = DisplayManager.getInstance().getDefaultDisplay(this);
        DisplayAttributes displaymetrics = dm.map(Display::getAttributes).orElse(null);
        float density = displaymetrics.densityPixels;
        int margin = (int) (density * MARGIN);
        DirectionalLayout container = (DirectionalLayout) findComponentById(ResourceTable.Id_container);

        mProgressPieViewCode = new ProgressPieView(getContext());
        final DirectionalLayout.LayoutConfig layoutParams
                = new DirectionalLayout.LayoutConfig(0, MATCH_PARENT, LayoutAlignment.CENTER, 1);
        layoutParams.setMargins(margin, margin, margin, margin);
        mProgressPieViewCode.setLayoutConfig(layoutParams);
        mProgressPieViewCode.setText("Code");
        mProgressPieViewCode.setTextSize(22);
        mProgressPieViewCode.setTextColor(ResUtil.getColor(getContext(), ResourceTable.Color_holo_orange_light));
        mProgressPieViewCode.setBackgroundColor(ResUtil.getColor(getContext(), ResourceTable.Color_holo_red_dark));
        mProgressPieViewCode.setProgressColor(ResUtil.getColor(getContext(), ResourceTable.Color_holo_green_dark));
        mProgressPieViewCode.setStrokeColor(ResUtil.getColor(getContext(), ResourceTable.Color_holo_blue_dark));
        mProgressPieViewCode.setStartAngle(720);
        container.addComponent(mProgressPieViewCode);

        // Default version
        mProgressPieView = (ProgressPieView) findComponentById(ResourceTable.Id_progressPieView);
        mProgressPieView.setOnProgressListener(new ProgressPieView.OnProgressListener() {
            @Override
            public void onProgressChanged(int progress, int max) {
                if (!mProgressPieView.isTextShowing()) {
                    mProgressPieView.setShowText(true);
                    mProgressPieView.setShowImage(false);
                }
            }

            @Override
            public void onProgressCompleted() {
                if (!mProgressPieView.isImageShowing()) {
                    mProgressPieView.setShowImage(true);
                }
                mProgressPieView.setShowText(false);
                mProgressPieView.setImageResource(ResourceTable.Media_ic_action_accept);
            }
        });

        // Inverted default version
        mProgressPieViewInverted = (ProgressPieView) findComponentById(ResourceTable.Id_progressPieViewInverted);

        // XML version
        mProgressPieViewXml = (ProgressPieView) findComponentById(ResourceTable.Id_progressPieViewXml);

        Slider mSlider = (Slider) findComponentById(ResourceTable.Id_slider);
        mSlider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                mProgressPieView.setProgress(i);
                mProgressPieView.setText(i + "%");
                mProgressPieViewInverted.setProgress(i);
                mProgressPieViewXml.setProgress(i);
                mProgressPieViewCode.setProgress(i);
            }

            @Override
            public void onTouchStart(Slider slider) {}

            @Override
            public void onTouchEnd(Slider slider) {}
        });

        findComponentById(ResourceTable.Id_anim).setClickedListener(component -> {
            mProgressPieView.setProgress(0);
            mProgressPieView.animateProgressFill();
            mProgressPieViewInverted.setProgress(0);
            mProgressPieViewInverted.animateProgressFill();
            mProgressPieViewXml.setProgress(0);
            mProgressPieViewXml.animateProgressFill();
            mProgressPieViewCode.setProgress(0);
            mProgressPieViewCode.animateProgressFill();
        });
    }
}
